<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>性别</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../../plugins/mui/mui.min.css">
		<script src="../../js/statistics.js"></script>
		<style type="text/css">
			 *{
            box-sizing: border-box;
            font-family: 'Microsoft YaHei';
	        }
	        body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, form,  input, p, table, th, td ,select,option{
	            padding: 0;
	            margin: 0;
	            list-style: none;
	        }
	        body{
	        	padding-top:45px;
	        	padding-bottom:10px;
	        }
	        .clearfix:before{
            display:table;
            content:'';
	        }
	        .clearfix:after{
	            display:table;
	            content:'';
	            clear:both;
	        }
			/*title的样式覆盖*/
			.mui-bar{
				background-color:#2a2c33;
				top:0;
			}
			.mui-bar>a{
				color:#fff;
			}
			.mui-title{
				color:#fff;
			}
			.mui-input-row:after{
				display:none;
			}
			.mui-input-group:before{
				display:none;
			}
			.mui-input-group:after{
				display:none;
			}
			#input1::-webkit-input-placeholder{
				line-height: 22px;
			}
			/*百分比优化*/
			.mui-input-clear{
				padding: 11px 4%;
			}
			
			/*列表边框取消*/
			.mui-table-view:before{
				display:none;
			}
			.mui-table-view:after{
				display:none;
			}
			
			/*百分比优化*/
			.mui-table-view-cell {
				padding: 11px 4%;
			}
			.mui-table-view-cell>a:not(.mui-btn){
				margin: -11px -4%;
			}
			/*选择钩子颜色替换*/
			.mui-table-view-radio .mui-table-view-cell .mui-navigate-right:after {
				
				color: #43ac45;
			}
		</style>
	</head>
	<body>
		<header class="mui-bar ">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">性别</h1>
		</header>
		
		<ul class="mui-table-view mui-table-view-radio" style='margin-top:10px;'>
				<li class="mui-table-view-cell" id='man'>
					<a class="mui-navigate-right" >
						男
					</a>
				</li>
				<li class="mui-table-view-cell " id='woman'>
					<a class="mui-navigate-right">
						女
					</a>
				</li>
			</ul>
		<script src="../../plugins/jquery/jquery-3.1.0.min.js"></script>
		<script src="../../plugins/mui/mui.min.js"></script>
		<script src="../../js/getApiToken.js"></script>
		<script src="../../js/md5.js"></script>
		<script type="text/javascript">
			
			mui.plusReady(function(){
				
				init();
				//初始化个性签名
				function init(){
					if((plus.storage.getItem('cg_sex')==0)||(plus.storage.getItem('cg_sex')==1)){
						 $('#man').addClass('mui-selected');
					}else{
						$('#woman').addClass('mui-selected');
					}
					//关闭等待框
					plus.nativeUI.closeWaiting();
					//显示当前页面
					mui.currentWebview.show('slide-in-right',200);
				}
				
				//保存判断用户名函数
				mui('#man')[0].addEventListener('tap',function(){
						if(!$(this).hasClass('mui-selected')){
							add('man');
						}
					})
				mui('#woman')[0].addEventListener('tap',function(){
					if(!$(this).hasClass('mui-selected')){
							add('woman');
					}
				})
				function add(a){
					if(a=='woman'){
						var b='2';
					}else{
						var b='1';
					}
					plus.nativeUI.showWaiting('保存中...');
					 myAjax('personal/user_profile/updateUserProfile', 'post', {
								'user_id': plus.storage.getItem('cg_user_id'),
								'user_token': plus.storage.getItem('cg_user_token'),
								'sex':b
						}, function(data) {
								plus.nativeUI.closeWaiting();
								if(data.success){
									 //修改本地性别
									plus.storage.setItem('cg_sex', b);
//									plus.storage.setItem('cg_headimgurl', data.headimgurl);
									if(data.headimgurl.indexOf('http://') == -1 && data.headimgurl.indexOf('https://') == -1){
										var path=_baseUrl+data.headimgurl;
									}else{
										var path=data.headimgurl;
									}
									plus.io.resolveLocalFileSystemURL("_doc/userImg/", function( entry ) {
										// 可通过entry对象操作test.html文件
										var directoryReader = entry.createReader();
										directoryReader.readEntries( function( entries ){
//									var i;
										for( var i=0; i < entries.length; i++ ) {
											entries[i].remove();//遍历每个文件
										}
											var dtask = plus.downloader.createDownload(_baseUrl+path, {filename:"_doc/userImg/"}, function(d,status){
												if ( status == 200 ) {
//											console.log(d.filename);
													plus.io.resolveLocalFileSystemURL(d.filename, function(entry) {
														entry.file( function ( file ) {
															plus.storage.setItem('cg_headimgurl', entry.toLocalURL());
															plus.storage.setItem('cg_sex', b);
															var main = plus.webview.getWebviewById('user.html');//或者通过A页面id获取A页面对象
															mui.fire(main, "pageflowrefresh" ,{'gender':b});//出发A页面的pageflowrefresh方法
															var main1 = plus.webview.getWebviewById('my-1.html');//或者通过A页面id获取A页面对象
															mui.fire(main1, "pageflowrefresh" ,{'gender':b});//出发A页面的pageflowrefresh方法
															mui.toast('性别修改成功');
															plus.webview.currentWebview().close();
														}, function ( e ) {
															alert( e.message );
														});
													});
												} else {
													plus.nativeUI.alert("头像下载失败！");
												}
											});
											dtask.start();
											//图片不存在的时候才下载本地图片
//									console.log(_baseUrl+data_path);

										}, function ( e ) {
											alert( "Read entries failed: " + e.message );
										} );


									}, function ( e ) {
										var dtask = plus.downloader.createDownload(_baseUrl+path, {filename:"_doc/userImg/"}, function(d,status){
											if ( status == 200 ) {
//											console.log(d.filename);
												plus.io.resolveLocalFileSystemURL(d.filename, function(entry) {
													entry.file( function ( file ) {
														plus.storage.setItem('cg_headimgurl', entry.toLocalURL());
														$('#mainImage')[0].src=plus.storage.getItem('cg_headimgurl');
													}, function ( e ) {
														alert( e.message );
													});
												});
											} else {
												plus.nativeUI.alert("头像下载失败！");
											}
										});
										dtask.start();
									} );


								}else{
									mui.toast(data.error_msg);
								}
						})
				}
			});
		</script>
	</body>
</html>


